package com.zcxy360.sandtable.controller;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.zcxy360.sandtable.domain.Plan;
import com.zcxy360.sandtable.service.PlanService;

/**
 * 教案接口
 * @author 盛亚辉
 *
 */
@RequestMapping("plan")
@RestController
public class PlanController {

	@Autowired
	private PlanService planService;
	
	/**
	 * 获取教案
	 * @param planId 教案id
	 * @param deep 0代表基本数据,1代表扩展数据
	 * @return 教案对象
	 */
	@RequiresAuthentication
	@GetMapping("{planId}")
	public Object getByPlanId(@PathVariable("planId") Integer planId,@RequestParam(defaultValue="0") Integer deep) {
		return  planService.getByPlanId(planId,deep);
	}
	
	/**
	 * 获取教案集合
	 * @param plan 教案字段
	 * @param key 关键字
	 * @param orderByClause 排序规则
	 * @param sign 查询标识
	 * @param pageNum 页码
	 * @param pageSize 页数
	 * @param navigatePages 导航数
	 * @return 教案集合
	 */
	@RequiresRoles(value={"admin","manager","teacher"},logical=Logical.OR)
	@GetMapping
	public Object list(Plan plan,String key,String orderByClause,Integer sign,
			@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "5") Integer pageSize,
			@RequestParam(defaultValue = "5") Integer navigatePages) {
		return planService.list(plan,key,orderByClause,sign,pageNum,pageSize,navigatePages);
	}
	
	/**
	 * 新增教案
	 * @param plan 教案字段
	 * @param file 上传文件
	 * @return 教案对象
	 */
	@RequiresRoles(value={"admin","teacher"},logical=Logical.OR)
	@PostMapping
	public Object post(MultipartFile file,Plan plan) {
		return planService.post(file,plan);
	}
	
	/**
	 * 更新教案
	 * @param plan 教案字段
	 * @param file 上传文件
	 * @return 布尔值
	 */
	@RequiresRoles(value={"admin","teacher"},logical=Logical.OR)
	@PutMapping
	public Object update(MultipartFile file,Plan plan) {
		return planService.update(file,plan);
	}
	
	/**
	 * 删除教案
	 * @param planIds 教案id数组
	 * @return 布尔值
	 */
	@RequiresRoles(value={"admin","manager","teacher"},logical=Logical.OR)
	@DeleteMapping("{planIds}")
	public Object delete(@PathVariable("planIds") int[] planIds) {
		return planService.delete(planIds);
	}
	
	/**
	 * 检查名称
	 * @param name 教案名称
 	 * @return 布尔值
	 */
	@RequiresRoles(value={"admin","teacher"},logical=Logical.OR)
	@GetMapping("checkName")
	public Object checkName(@RequestParam("name") String name) {
		return planService.checkName(name);
	}
	
}
